﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using DXApplicationTangche.UC.成品出库单.service;
using DXApplicationTangche.UC.盘点.Service;
using DXApplicationTangche.Const;
using DevExpress.XtraGrid.Demos.util;

namespace DXApplicationTangche.UC.盘点.盘点UC
{
    public partial class XtraUserControl盘盈处理 : DevExpress.XtraEditors.XtraUserControl
    {
        private StockStoreInventoryPlanDTO stockStoreInventoryPlanDTO = new StockStoreInventoryPlanDTO();
        private String stockStoreInventoryPlanCode;
        private List<StockInOutDetailDTO> stock正处理 = new List<StockInOutDetailDTO>();
        private List<StockInventoryPlanResultDTO> stock已处理 = new List<StockInventoryPlanResultDTO>();
        private List<StockStoreInventoryResultDTO> stock待处理 = new List<StockStoreInventoryResultDTO>();
        private String mnemonic;
        private String name;
        public XtraUserControl盘盈处理()
        {
            InitializeComponent();
        }
        public XtraUserControl盘盈处理(StockStoreInventoryPlanDTO stockStoreInventoryPlanDTO)
        {
            InitializeComponent();
            this.stockStoreInventoryPlanCode = stockStoreInventoryPlanDTO.stockStoreInventoryPlanCode;
            this.stockStoreInventoryPlanDTO = stockStoreInventoryPlanDTO;
            this.searchLookUpEdit调拨入库销售员.Properties.DataSource = 成品出库单Service.getSalerEmployeeShorts();
            this.getData();
            this.refreshDate();
        }
        #region 选择调拨入库销售员
        private void searchLookUpEdit调拨入库销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear调拨入库销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit调拨入库销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit调拨入库销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.mnemonic = this.searchLookUpEdit调拨入库销售员.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.name = this.searchLookUpEdit调拨入库销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear调拨入库销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit调拨入库销售员.ToolTip = null;
            searchLookUpEdit调拨入库销售员.EditValue = null;
        }
        private void searchLookUpEdit调拨入库销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.name;
        }
        #endregion

        private void getData()
        {
            this.stock待处理 = 盘点Service.getStockInventorySurplusAll(stockStoreInventoryPlanCode);
            this.stock已处理 = 盘点Service.getStockInventoryAllTreated(stockStoreInventoryPlanCode, "盘盈");
        }
        private void refreshDate()
        {
            this.gridControl待处理.DataSource = this.stock待处理;
            this.gridView待处理.RefreshData();
            this.gridControl已处理.DataSource = this.stock已处理;
            this.gridView已处理.RefreshData();
            this.gridView已处理.BestFitColumns();
            this.gridControl正处理.DataSource = this.stock正处理;
            this.gridView正处理.RefreshData();
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.mnemonic))
            {
                MessageBox.Show("请填写完整");
                return;
            }
            try
            {
                if (this.checkBox是否盘盈入库.Checked)
                {
                    盘点Service.saveStockInventoryResult(
                    this.stockStoreInventoryPlanCode,
                    "1",
                    StockInOutTypeConst.StockInventoryIn,
                    stockStoreInventoryPlanDTO.storeId,
                    stockStoreInventoryPlanDTO.storeName,
                    "",
                    "",
                    this.mnemonic,
                    this.richTextBox备注.Text.Trim(),
                    this.stock正处理
                    );
                    MessageBox.Show("成功");
                    this.stock正处理.Clear();
                    this.getData();
                    this.refreshDate();
                }
                else
                {
                    if(盘点Service.saveStockInventoryResultDone(
                        this.stockStoreInventoryPlanCode,
                        this.mnemonic,
                        this.richTextBox备注.Text.Trim(),
                        "",
                        "1",
                        this.stock正处理
                        ))
                    {
                        this.richTextBox备注.Clear();
                        MessageBox.Show("成功");
                        this.stock正处理.Clear();
                        this.getData();
                        this.refreshDate();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("失败" + ex.Message);
            }        
        }

        private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (StockInOutDetailDTO dTO in this.stock正处理)
            {
                if (dTO.barcode.Equals(FunctionHelper.GetgridViewValue(this.gridView正处理, this.gridView正处理.FocusedRowHandle, "barcode")))
                {
                    this.stock正处理.Remove(dTO);
                    this.refreshDate();
                    break;
                }
            }
        }

        private void 全部移除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.stock正处理.Clear();
            this.refreshDate();
        }

        private void gridView待处理明细_DoubleClick(object sender, EventArgs e)
        {
            //foreach(StockStoreInventoryResultDTO dTO in this.stock待处理)
            //{
            //    foreach(StockInOutDetailDTO stockdto in dTO.stockInOutDetailDTOS)
            //    {
            //        if (stockdto.Equals(FunctionHelper.GetgridViewValue(this.gridView待处理明细, this.gridView待处理明细.FocusedRowHandle, "barcode")))
            //        {
            //            if (!this.stock正处理.Exists(dto => dto.barcode.Equals(stockdto.barcode)))
            //            {
            //                this.stock正处理.Add(stockdto);
            //                this.refreshDate();
            //                return;
            //            }
            //        }
            //    }
            //}
        }

        private void gridView待处理_DoubleClick(object sender, EventArgs e)
        {
            String str = FunctionHelper.GetgridViewValue(this.gridView待处理, this.gridView待处理.FocusedRowHandle, "styleNameCn") +
                FunctionHelper.GetgridViewValue(this.gridView待处理, this.gridView待处理.FocusedRowHandle, "styleSizeCd") +
                FunctionHelper.GetgridViewValue(this.gridView待处理, this.gridView待处理.FocusedRowHandle, "fabricCode");
            foreach (StockStoreInventoryResultDTO dTO in this.stock待处理)
            {
                if (str.Equals(dTO.buildNames()))
                {
                    Form盘点处理选择 form = new Form盘点处理选择(dTO.stockInOutDetailDTOS);
                    form.ShowDialog();
                    if (form.if确认 && form.stock已选择 != null && 0 != form.stock已选择.Count)
                    {
                        List<String> barcode = new List<string>();
                        foreach (StockInOutDetailDTO item in this.stock正处理)
                        {
                            barcode.Add(item.barcode);
                        }
                        foreach (StockInOutDetailDTO stock in form.stock已选择)
                        {
                            if (!barcode.Contains(stock.barcode))
                            {
                                this.stock正处理.Add(stock);
                            }
                        }
                        //this.stock正处理.AddRange(form.stock已选择);
                    }
                    this.refreshDate();
                    return;
                }
            }
        }
    }
}
